package com.dongxing.concyrrency.chapter7;

import java.util.concurrent.TimeUnit;

/**
 * @author XavierDong
 **/
public class TicketWindowRunnable implements Runnable {

    private int index = 1;

    private final static int MAX = 500;

    private final Object MONITOR = new Object();

    @Override
    public void run() {

        while (true) {
            // 单线程执行
            // 1
            synchronized (MONITOR) {
                if (index > MAX) break;
                try {
                    TimeUnit.MILLISECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + " 的号码是： " + (index++));
            }
            // 2
        }

    }
}
